package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.core.page.TableDataInfo;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.common.utils.SecurityUtils;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerCoupon;
import com.huohuzhihui.merchant.domain.MerCouponGet;
import com.huohuzhihui.merchant.exception.CouponCheckException;
import com.huohuzhihui.merchant.service.IMerCouponGetService;
import com.huohuzhihui.merchant.service.IMerCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 优惠券Controller
 *
 * @author zylu
 * @date 2020-11-14
 */
@RestController
@RequestMapping("/api/coupon")
public class ApiCouponController extends BaseController {
    @Autowired
    private IMerCouponGetService merCouponGetService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IMerCouponService merCouponService;

    /**
     * 查询我的优惠券
     *
     * @param userId
     * @return
     */
    @RequestMapping("/findCouponListByUserId")
    @ResponseBody
    public AjaxResult findCouponListByUserId(Long userId) {
        MerCouponGet merCouponGet = new MerCouponGet();
        merCouponGet.setUserId(userId);
        List<MerCouponGet> merCouponGetList = this.merCouponGetService.selectMerCouponGetList(merCouponGet);
        return AjaxResult.success("查询优惠券成功", merCouponGetList);
    }

    /**
     * 优惠券核销
     *
     * @param
     * @return
     */
    @RequestMapping("/checkCoupon")
    @ResponseBody
    public AjaxResult checkCoupon(HttpServletRequest request, String couponCode) {

        try {
            LoginUser loginUser = tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();

            int result = this.merCouponGetService.checkCouponByCouponCode(couponCode);
            return AjaxResult.success("优惠券核销成功", result);
        } catch (CouponCheckException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询已核销的优惠券
     *
     * @param
     * @return
     */
    @RequestMapping("/findCouponCheckList")
    @ResponseBody
    public TableDataInfo findCouponCheckList(HttpServletRequest request) {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();
        MerCouponGet merCouponGet = new MerCouponGet();
        merCouponGet.setMerchantId(sysUser.getDeptId());
        merCouponGet.setUseStatus(1);
        List<MerCouponGet> merCouponGetList = this.merCouponGetService.selectMerCouponGetList(merCouponGet);
        return getDataTable(merCouponGetList);
    }


    /**
     * 商家端查询优惠券
     *
     * @param request
     * @return
     */
    @RequestMapping("/findMerchantCouponList")
    @ResponseBody
    public AjaxResult findMerchantCouponList(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getUser();

        MerCoupon merCoupon = new MerCoupon();
        merCoupon.setMerchantId(sysUser.getDeptId());

        List<MerCoupon> merCouponList = this.merCouponService.selectMerCouponList(merCoupon);
        return AjaxResult.success("商家端查询优惠券成功", merCouponList);
    }

    /**
     * 商户手机端添加优惠券
     *
     * @param
     * @return
     */
    @RequestMapping("/addCoupon")
    @ResponseBody
    public AjaxResult addCoupon(HttpServletRequest request, MerCoupon merCoupon) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();
            Long merchantId = sysUser.getDeptId();

            merCoupon.setMerchantId(merchantId);
            merCoupon.setCreateBy(SecurityUtils.getUsername());
            return toAjax(merCouponService.insertMerCoupon(merCoupon));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 商户手机端修改优惠券
     *
     * @param
     * @return
     */
    @RequestMapping("/editCoupon")
    @ResponseBody
    public AjaxResult edit(@RequestBody MerCoupon merCoupon) {
        try {
            merCoupon.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(merCouponService.updateMerCoupon(merCoupon));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 商户手机端查询优惠券详情
     *
     * @param
     * @return
     */
    @RequestMapping("/getCouponById")
    @ResponseBody
    public AjaxResult getCouponById(Long id) {
        try {
            return AjaxResult.success(merCouponService.selectMerCouponById(id));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
